<?php 

$con=new mysqli("localhost","root","","blog") or die("<p>connect failed</P>");
$con->set_charset("utf8");
$table=$con->query("select * from usr");



// var_dump($table);
    // header('Content-type:text/html; charset=utf-8');
    
	session_start();
 

	if (isset($_POST)) {

		$usrname = htmlentities(trim($_POST['usrname']));
		$passwd = htmlentities(trim($_POST['passwd']));

		if (($usrname == '') || ($passwd == '')) {
			header('refresh:3; url=login.php');
			echo "用户名或密码不能为空，系统将在3秒后返回注册界面，请重新注册";
			exit;
        } 

        while($record=$table->fetch_assoc()){
            if ($usrname == $record['usrname']) {
                header('refresh:3; url=login.php');
                echo "该用户名已存在，系统将在3秒后返回注册界面，请重新注册";
                exit;
            }
        }

        // $con->query("insert into usr values ( $usrname, $passwd)");
       $thing = $con->prepare("insert into usr (usrname,passwd) values (?, ?)");
       $thing->bind_param("ss",$usrname,$passwd);
       $thing->execute();

        $_SESSION['usrname'] = $usrname;
        $_SESSION['islogin'] = 1;


        setcookie('usrname', $usrname, time()+7*24*60*60);
        setcookie('code', md5($usrname.md5($passwd)), time()+7*24*60*60);
        
        

        
        header('location:index.php?id='.$usrname);
        exit;
             
        
        

    }
$con->close();
 ?>
